package com.xgry.bpm.api.business.coding.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xgry.bpm.api.business.coding.dao.CodingDao;
import com.xgry.bpm.api.business.coding.entity.Coding;
import com.xgry.bpm.api.business.coding.form.CodingForm;
import com.xgry.bpm.api.business.coding.service.CodingService;
import com.xgry.bpm.api.business.coding.vo.CodingVO;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.List;


/**
 * <p>
 * Coding服务实现类
 * </p>
 *
 * @author admin
 * @since 2020-07-16
 */
@Service
        public class CodingServiceImpl extends ServiceImpl<CodingDao, Coding>implements CodingService {

        @Override
        public int save(CodingForm record){
            Coding data=this.convertDO(record);
            int insert = baseMapper.insert(data);
            record.setId(data.getId());
            return insert;
        }

        @Override
        public int updateById(CodingForm record){
        //修改时间
    Coding data=this.convertDO(record);
        return baseMapper.updateById(data);
        }

        @Override
        public int deleteById(String id){
        return baseMapper.deleteById(id);
        }

        @Override
        public CodingVO selectById(String id){
        Coding data=baseMapper.selectById(id);
        CodingVO vo=new CodingVO();
        BeanUtils.copyProperties(data,vo);
        return vo;
        }

        @Override
        public List<CodingVO>selectAll(CodingForm record){
    Coding data=this.convertDO(record);
        QueryWrapper<Coding>queryWrapper=new QueryWrapper<>();
        queryWrapper.setEntity(data);
        List<Coding>list=baseMapper.selectList(queryWrapper);
        return this.convert(list);
        }

        @Override
        public IPage<CodingVO>selectPage(CodingForm record){
        // form -> do 转换
    Coding data=this.convertDO(record);
        // 分页数据设置
        Page<Coding>page=new Page<>(record.getCurrent(),record.getSize());
        // 查询条件
        QueryWrapper<Coding>queryWrapper=new QueryWrapper<>();
        queryWrapper.setEntity(data);
        IPage<Coding>list=baseMapper.selectPage(page,queryWrapper);
        IPage<CodingVO>iPage=new Page<>();
        iPage.setRecords(this.convert(list.getRecords()));
        iPage.setCurrent(list.getCurrent());
        iPage.setSize(list.getSize());
        iPage.setTotal(list.getTotal());
        iPage.setPages(list.getPages());
        return iPage;
        }

        @Override
        public IPage<Coding>getPage(IPage<Coding>page,QueryWrapper<Coding>queryWrapper){
        return baseMapper.selectPage(page,queryWrapper);
        }


        /**
        * Form -> Do
        * @param form 对象
        * @return Do对象
        */
        private Coding convertDO(CodingForm form){
            Coding data=new Coding();
            BeanUtils.copyProperties(form,data);
        return data;
        }

        /**
        * Do -> VO
        * @param list 对象
        * @return VO对象
        */
        private List<CodingVO>convert(List<Coding>list){
        List<CodingVO> codingList=new ArrayList<>();
        if(CollectionUtils.isEmpty(list)){
        return codingList;
        }
        for(Coding source:list){
    CodingVO target=new CodingVO();
        BeanUtils.copyProperties(source,target);
    codingList.add(target);
        }
        return codingList;
        }

        }
